﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace Vision.Common
{
    /// <summary>
    /// 子区
    /// </summary>
    public class NodeComponent : CDrawObj
    {
        public int ID = 0;
        public string Name { get => $"子区{ID}"; }
        public CDrawPoint CurPt = new CDrawPoint();    //基准点
        public float CurAngle { get; set; }   //角度

        public override TrackerState State { get => m_state;
            set
            {
                m_state = value;
                CurPt.State = value;
                foreach (var item in elements)
                {
                    item.State = value == TrackerState.active?TrackerState.selected:value;
                }
            }
        } //属性


        //private PointF baseBasePt;
        /// <summary>
        /// 原点
        /// </summary>
        public PointF BasePt { get; set; } = default;
        //角度
        public float BaseAngle { get; set; } = 0;   

        //复位至初始值
        public void Reset(bool bBase2Cur = true)
        {
            if (bBase2Cur)
            {
                CurPt.Pt = BasePt;
                CurAngle = BaseAngle;
            }
            else
            {
                BasePt = CurPt.Pt;
                BaseAngle = CurAngle;
            }
            foreach(var item in elements)
            {
                item.Reset(bBase2Cur);
            }
        }

        //public int ElMaxID = 0;
        //public int NextElementID { get => ++elMaxID; }
        private List<NodeElement> elements = new List<NodeElement>();    //元件
        public List<NodeElement> Elements { get=> elements; set=> elements=value; }
        private bool selected = false;
        public int GetMaxID()
        {
            int maxID = 0;
            if (elements.Count > 0)
                maxID = elements.Max(p => p.ID);
            return maxID;
        }
        public new NodeComponent Copy()
        {
            NodeComponent node = new NodeComponent();
            Copy(node);
            return node;
        }
        public void Copy(NodeComponent node)
        {
            base.CopyTo(node);
            CurPt.CopyTo(node.CurPt);
            node.ID = ID;
            node.CurAngle = CurAngle;
            //node.ElMaxID = ElMaxID;
            node.selected = selected;
            node.elements.Clear();
            foreach (var item in elements)
            {
                node.elements.Add(item.Copy());
            }
        }
        //public void Select(bool isSelected)
        //{
        //    selected = isSelected;
        //    basePt.State = isSelected ? TrackerState.selected : TrackerState.normal;
        //    foreach (var item in elements)
        //        item.Select(isSelected);
        //}

        public override void Move(float dx, float dy)
        {
            CurPt.Pt = new PointF(CurPt.Pt.X + dx, CurPt.Pt.Y + dy);
            foreach (var item in elements)
                item.Move(dx, dy);
        }
        
        public void AddElement(NodeElement node)
        {
            elements.Add(node);
        }
        public void RemoveElement(NodeElement node)
        {
            elements.Remove(node);
        }
        public override void Draw(Graphics g, float scale = 1)
        {
            CurPt?.Draw(g, scale);
            foreach (var item in elements)
                item.Draw(g, scale);
        }

        public override TrackerInfo GetTrack(PointF pt, float scale)
        {
            return CurPt.GetTrack(pt,scale);
        }

        public override void ReadSelfAttrbutes(XmlReader reader)
        {
            base.ReadSelfAttrbutes(reader);
            //if(GlobalSysVar.PrjVersion<1)
            //    ReadPrjV0(reader);
            //else
                ReadPrjV1(reader);
        }
        private void ReadPrjV0(XmlReader reader)
        {
            string Name = reader.GetAttribute("Name");
            ID = Convert.ToInt32(Name.Substring(2,Name.Length-2));
            float x = Convert.ToSingle(reader.GetAttribute("X"));
            float y = Convert.ToSingle(reader.GetAttribute("Y"));

            BasePt = new PointF(x, y);
            BaseAngle = Convert.ToSingle(reader.GetAttribute("Angle"));
            CurPt.Pt = BasePt;
            CurAngle = BaseAngle;

            //ElMaxID = Convert.ToInt32(reader.GetAttribute("elementID"));
            if (reader.ReadToDescendant("Elements"))
            {
                if (reader.ReadToDescendant("Element"))
                {
                    do
                    {
                        NodeElement nodeElement = new NodeElement();
                        nodeElement.ReadSelfAttrbutes(reader);
                        elements.Add(nodeElement);
                    } while (reader.ReadToNextSibling("Element"));
                }
            }
            reader.ReadEndElement();
        }
        private void ReadPrjV1(XmlReader reader)
        {
            string strID = reader.GetAttribute("ID");
            ID = Convert.ToInt32(strID);
            float x = Convert.ToSingle(reader.GetAttribute("X"));
            float y = Convert.ToSingle(reader.GetAttribute("Y"));

            BasePt = new PointF(x, y);
            BaseAngle = Convert.ToSingle(reader.GetAttribute("Angle"));
            CurPt.Pt = BasePt;
            CurAngle = BaseAngle;

            //ElMaxID = Convert.ToInt32(reader.GetAttribute("elementID"));
            if (reader.ReadToDescendant("Elements"))
            {
                if (reader.ReadToDescendant("Element"))
                {
                    do
                    {
                        NodeElement nodeElement = new NodeElement();
                        nodeElement.ReadSelfAttrbutes(reader);
                        elements.Add(nodeElement);
                    } while (reader.ReadToNextSibling("Element"));
                }
                reader.Read();
            }
            //reader.ReadEndElement();
        }
        //public override void WriteSelfAttrbutes(XmlWriter writer)
        //{
        //    base.WriteSelfAttrbutes(writer);
        //    writer.WriteAttributeString("Name", Name);
        //    writer.WriteAttributeString("X", basePt.Pt.X.ToString());
        //    writer.WriteAttributeString("Y", basePt.Pt.Y.ToString());
        //    writer.WriteAttributeString("Angle", Angle.ToString());
        //    writer.WriteAttributeString("elementID", elMaxID.ToString());
        //    writer.WriteStartElement("Elements");
        //    foreach (var item in elements)
        //    {
        //        writer.WriteStartElement("Element");
        //        item.WriteSelfAttrbutes(writer);
        //        writer.WriteEndElement();
        //    }
        //    writer.WriteEndElement();
        //}
        public override void WriteSelfAttrbutes(XmlWriter writer)
        {
            base.WriteSelfAttrbutes(writer);
            writer.WriteAttributeString("ID", ID.ToString());

            writer.WriteAttributeString("X", BasePt.X.ToString());
            writer.WriteAttributeString("Y", BasePt.Y.ToString());
            writer.WriteAttributeString("Angle", BaseAngle.ToString());

            writer.WriteAttributeString("count", elements.Count.ToString());
            writer.WriteStartElement("Elements");
            foreach (var item in elements)
            {
                writer.WriteStartElement("Element");
                item.WriteSelfAttrbutes(writer);
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
        }
    }
}